package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.py.mymodule.submodule.entity.Costomer;
import org.py.mymodule.submodule.entity.Costomerform;
import org.py.mymodule.submodule.common.Result;
import org.py.mymodule.submodule.mapper.CostomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-13
 */
@RestController
@RequestMapping("/costomer")
@MapperScan("org.py.mymodule.submodule.mapper")
public class CostomerController {
    @Autowired
    private CostomerMapper costomerMapper;

    //用户的添加
    //用户的添加
    @PostMapping("/add")
    public Result addUser(@RequestBody Costomerform costomerform) {

        if (StringUtils.isBlank(costomerform.getCostomerType())) {
            costomerform.setCostomerType("非会员");
        }
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("costomer_phone", costomerform.getCostomerPhone());
        Costomer test = costomerMapper.selectOne(queryWrapper);
        if (test != null) {
            return Result.error("该电话号码已经注册，请重新输入");
        } else {
            Costomer costomer = new Costomer(costomerform.getCostomerName(), costomerform.getCostomerType(), costomerform.getCostomerAddress(), costomerform.getCostomerPhone(), costomerform.getCostomerIntegral());
            int rows = costomerMapper.insert(costomer);
            if (rows > 0) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        }

    }

    // 用户的查询
    @GetMapping("/get/{id}")
    public List<Costomer> getUserById(@PathVariable("id") Long id) {
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("costomer_id", id);
        List<Costomer> costomer = costomerMapper.selectList(queryWrapper);
        if (costomer != null) {
            return costomer;
        } else {

            throw new RuntimeException("Costomer with ID " + id + " not found");

        }
    }

    //查询所有用户
    @GetMapping("getall")
    public List<Costomer> getallUser() {
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        return costomerMapper.selectList(queryWrapper);

    }

    //用户的查询，按电话
    @GetMapping("/getbyphone")
    public Costomer getUserbyphone(@RequestParam String phone) {
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("costomer_phone", phone);
        Costomer costomer = costomerMapper.selectOne(queryWrapper);
        if (costomer != null) {
            return costomer;
        } else {
            throw new RuntimeException("Costomer with PHONE " + phone + " not found");
        }
    }

    //
    // 用户的修改
    @PutMapping("/update/{id}")
    public String updateUser(@PathVariable("id") Long id, @RequestBody Costomer costomer) {
        Costomer existingCostomer = costomerMapper.selectById(id);
        if (existingCostomer == null) {
            return "Costomer with id " + costomer.getCostomerId() + " not exists";
        } else {
            costomerMapper.updateById(costomer);
            return "修改成功";
        }
    }


    // 用户的删除
    @DeleteMapping("/deletebyphone")
    public String deleteUser(@RequestParam String phone) {
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("costomer_phone", phone);

        int rows = costomerMapper.delete(queryWrapper);

        if (rows > 0) {
            return "用户删除成功";
        } else {
            return "用户删除失败";
        }
    }

    //用户信息的更新
    //用户信息的更新
    @PutMapping("/update")
    public Result updateUser(@RequestBody Costomer costomer) {
        QueryWrapper<Costomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("costomer_id",costomer.getCostomerId());
        int result = costomerMapper.update(costomer,queryWrapper);;
        if(result>0){
            return Result.success();
        }else{
            return Result.error();
        }
    }

}